﻿namespace Hidistro.UI.Common.Controls
{
    using Hidistro.Core;
    using Hidistro.Membership.Context;
    using Hidistro.Membership.Core.Enums;
    using Hidistro.SaleSystem.Member;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using System;
    using System.IO;
    using System.Net;
    using System.Text;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;

    [PersistChildren(false), ParseChildren(true)]
    public abstract class VMemberTemplatedNewWebControl : VshopTemplatedWebControl
    {
        protected VMemberTemplatedNewWebControl()
        {
            if (!(HiContext.Current.User is Member))
            {
              
            }
        }

        private string GetResponseResult(string url)
        {
            using (HttpWebResponse response = (HttpWebResponse) WebRequest.Create(url).GetResponse())
            {
                using (Stream stream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                    {
                        return reader.ReadToEnd();
                    }
                }
            }
        }

        private void SkipWinxinOpenId(string userName, string openId)
        {
            string generateId = Globals.GetGenerateId();
            Member member = new Member(UserRole.Member) {
                GradeId = MemberProcessor.GetDefaultMemberGrade(),
                Username = userName,
                OpenId = openId,
                Email = generateId + "@localhost.com",
                SessionId = generateId,
                Password = generateId,
                PasswordFormat = MembershipPasswordFormat.Hashed,
                TradePasswordFormat = MembershipPasswordFormat.Hashed,
                TradePassword = generateId,
                IsApproved = true,
                RealName = string.Empty,
                Address = string.Empty
            };
            if (MemberProcessor.CreateMember(member) != CreateUserStatus.Created)
            {
                member.Username = "微信会员_" + generateId;
                member.Email = generateId + "@localhost.com";
                member.Password = member.TradePassword = generateId;
                MemberProcessor.CreateMember(member);
            }
            HttpCookie cookie = new HttpCookie("Vshop-Member") {
                Value = Globals.UrlEncode(member.Username),
                Expires = DateTime.Now.AddYears(1)
            };
            HttpContext.Current.Response.Cookies.Add(cookie);
            HiContext.Current.User = member;
            member.OnLogin();
        }
    }
}

